var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
define(["require", "exports", "core/BaseWidget.class", "core/Functions.module"], function (require, exports, BaseWidget, Functions) {
    "use strict";
    var FullPanel = (function (_super) {
        __extends(FullPanel, _super);
        function FullPanel(settings, panelOpinions) {
            var _this = _super.call(this, settings) || this;
            _this.baseClass = "widget-fullpanel";
            _this.isInnerWidgetLoaded = false;
            _this.isFold = false;
            _this.bottom = 0;
            _this.left = 0;
            _this.fulpanelheight = 0;
            _this.titles = [];
            _this.panelTitle = panelOpinions.panelTitle;
            _this.panelUniqClassName = panelOpinions.panelUniqClassName;
            _this.baseClass += '-' + _this.panelUniqClassName;
            _this.innerWidgetPath = panelOpinions.innerWidgetPath;
            _this.innerWidgetMain = panelOpinions.innerWidgetMain || '/Widget';
            return _this;
        }
        FullPanel.prototype.startup = function () {
            this.initContainer();
            this.setHtml(_.template(this.template.split('$$')[1])({
                title: this.panelTitle,
                uniqClass: this.baseClass
            }), '.widget-fullpanel-tit-ul');
            this.titles.push(this.panelTitle);
            if (this.titles.length > 9) {
            }
            this.setHtml(_.template(this.template.split('$$')[2])({
                uniqClass: this.baseClass
            }), '.widget-fullpanel-con');
            this.configure();
            this.interactable();
            this.showLoading();
            this.initInnerWidget(this.innerWidgetPath, this.innerWidgetMain);
        };
        FullPanel.prototype.configure = function () {
            this.loadingObj = this.domObj.find('.loading');
        };
        FullPanel.prototype.initContainer = function () {
            this.panelContainerWidget = $('#' + this.config.panelContainerID);
            if (this.panelContainerWidget.length > 0) {
            }
            else {
                this.template.split('$$')[0];
                this.panelContainerWidget = $('<div id="' + this.config.panelContainerID + '">' + this.template.split('$$')[0] + '</div>');
                $(this.AppX.appConfig.mainContainer).append(this.panelContainerWidget);
            }
            this.mainContainerObj = $(this.AppX.appConfig.mainContainer);
        };
        FullPanel.prototype.initInnerWidget = function (widgetPath, widgetMain) {
            require({}, [Functions.concatUrl(widgetPath, widgetMain)], function (Widget) {
                this.widgetObj = new Widget({
                    widgetPath: widgetPath,
                    panelSelector: 'div.' + this.baseClass,
                    readyCallback: this.onInnerWidgetReady.bind(this),
                    afterDestroyCallback: this.destroy.bind(this)
                });
            }.bind(this));
        };
        FullPanel.prototype.onInnerWidgetReady = function () {
            this.isInnerWidgetLoaded = true;
            this.hideLoading();
            this.domObj.find('.panel-content').show();
            setTimeout(this.rebuildHeight.bind(this), 1000);
            this.ready();
        };
        FullPanel.prototype.showLoading = function () {
            var _this = this;
            setTimeout(function () {
                if (!_this.isInnerWidgetLoaded) {
                    _this.loadingObj.show();
                }
            }, this.config.loadingDelay);
        };
        FullPanel.prototype.hideLoading = function () {
            this.loadingObj.remove();
        };
        FullPanel.prototype.rebuildHeight = function () {
            var domObjHeight = this.domObj.height(), domObjWidth = this.domObj.width(), mainContainerHeight = this.mainContainerObj.height();
        };
        FullPanel.prototype.interactable = function () {
            var _this = this;
            var that = this;
            var windowObj = $(window);
            var tits = $('.widget-fullpanel-tit-li');
            var cons = $('.widget-fullpanel-con-div');
            if (tits.length != cons.length) {
                return;
            }
            for (var i = 0; i < tits.length; i++) {
                tits[i].id = i.toString();
                tits[i].onclick = function () {
                    for (var j = 0; j < tits.length; j++) {
                        $(tits[j]).removeClass('select');
                        $(cons[j]).removeClass('select');
                        cons[j].style.display = 'none';
                    }
                    $(this).addClass('select');
                    $(cons[this.id]).addClass('select');
                    cons[this.id].style.display = 'block';
                };
                if (i < tits.length - 1) {
                    $(tits[i]).removeClass('select');
                    $(cons[i]).removeClass('select');
                    cons[i].style.display = 'none';
                }
            }
            $(this.domObj[0]).find('.glyphicon').on('click', function (e) {
                e.stopPropagation();
                var id = _this.domObj[0].id;
                if (_this.widgetObj) {
                    _this.widgetObj.destroy.bind(_this.widgetObj)();
                }
                else {
                    _this.destroy();
                }
                _this.reBindOnclickEvent();
                var lis = $('.widget-fullpanel-tit-li');
                var mods = $('.widget-fullpanel-con-div');
                if (lis.length > 0) {
                    lis.each(function (i, value) { value.id = i.toString(); });
                    for (var j = 0; j < lis.length; j++) {
                        $(lis[j]).removeClass('select');
                        $(mods[j]).removeClass('select');
                        mods[j].style.display = 'none';
                    }
                    if (parseInt(id) > 0) {
                        $(lis[parseInt(id) - 1]).addClass('select');
                        $(mods[parseInt(id) - 1]).addClass('select');
                        $(mods[parseInt(id) - 1]).css({ display: 'block' });
                    }
                    else {
                        $(lis[0]).addClass('select');
                        $(mods[0]).addClass('select');
                        $(mods[0]).css({ display: 'block' });
                    }
                }
                else {
                    $('#' + _this.config.panelContainerID).remove();
                }
            });
            $('.fullpanel_close').off().on('click', function (e) {
                for (var p in this.AppX.runtimeConfig.SideMenu.fullpanels) {
                    if (this.AppX.runtimeConfig.SideMenu.fullpanels[p] != null) {
                        this.AppX.runtimeConfig.SideMenu.fullpanels[p].destroyWidget();
                    }
                }
            }.bind(this));
            $('.fullpanel_fold').off().click(function (e) {
                if ($(e.currentTarget).find(' span').hasClass('glyphicon-chevron-down') === true) {
                    if (_this.isFold == true)
                        return;
                    _this.Fold();
                    $(e.currentTarget).find('span').removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-up');
                }
                else {
                    if (_this.isFold == false)
                        return;
                    _this.Unfold();
                    $(e.currentTarget).find('span').removeClass('glyphicon-chevron-up').addClass('glyphicon-chevron-down');
                }
            });
            windowObj.off().on('resize.widget-fullpanel', function () {
                var fullpanel = $('.widget-fullpanel');
                this.fulpanelheight = fullpanel.height();
                var maincontainer = $('#mainContainer');
                this.fulpanelheight = maincontainer.height();
                if (this.isFold) {
                    fullpanel.css({
                        height: 50,
                        top: this.fulpanelheight - 50
                    });
                }
                else {
                    fullpanel.css({
                        height: this.fulpanelheight,
                        top: "0px"
                    });
                }
            }.bind(this));
        };
        FullPanel.prototype.removeEvents = function () {
            $(window).off('resize.fullpanel');
        };
        FullPanel.prototype.reBindOnclickEvent = function () {
            var tits = $('.widget-fullpanel-tit-li');
            var cons = $('.widget-fullpanel-con-div');
            if (tits.length != cons.length) {
                return;
            }
            for (var i = 0; i < tits.length; i++) {
                tits[i].id = i.toString();
                tits[i].onclick = function () {
                    for (var j = 0; j < tits.length; j++) {
                        $(tits[j]).removeClass('select');
                        $(cons[j]).removeClass('select');
                        cons[j].style.display = 'none';
                    }
                    $(this).addClass('select');
                    $(cons[this.id]).addClass('select');
                    cons[this.id].style.display = 'block';
                };
                if (i < tits.length - 1) {
                    $(tits[i]).removeClass('select');
                    $(cons[i]).removeClass('select');
                    cons[i].style.display = 'none';
                }
            }
        };
        FullPanel.prototype.focus = function (autoFocus) {
            var tits = $('.widget-fullpanel-tit-li');
            var cons = $('.widget-fullpanel-con-div');
            var id = this.domObj[0].id;
            for (var j = 0; j < tits.length; j++) {
                if (j == parseInt(id)) {
                    $(tits[j]).addClass('select');
                    $(cons[j]).addClass('select');
                    cons[j].style.display = 'block';
                }
                else {
                    $(tits[j]).removeClass('select');
                    $(cons[j]).removeClass('select');
                    cons[j].style.display = 'none';
                }
            }
        };
        FullPanel.prototype.Close = function () {
            this.domObj.hide();
        };
        FullPanel.prototype.fold = function () {
            this.Fold();
        };
        FullPanel.prototype.Fold = function () {
            var fullpanel = $('.widget-fullpanel');
            var top = fullpanel.height() - 50;
            this.fulpanelheight = fullpanel.height();
            fullpanel.css({ top: top + "px" });
            this.isFold = true;
        };
        FullPanel.prototype.unfold = function () {
            this.Unfold();
        };
        FullPanel.prototype.Unfold = function () {
            var fullpanel = $('.widget-fullpanel');
            fullpanel.css({ top: "0", height: this.fulpanelheight });
            this.isFold = false;
        };
        FullPanel.prototype.destroy = function () {
            var _this = this;
            _super.prototype.destroy.call(this);
            this.titles.forEach(function (element) {
                if (element == $(_this.domObj[0]).find(".li_tit").data("text")) {
                    _this.titles.splice(element, 1);
                }
            });
            this.removeEvents();
            this.domObj.remove();
            this.widgetObj = null;
            this.focus(true);
            this.afterDestroy();
        };
        FullPanel.prototype.destroyWidget = function () {
            if (this.widgetObj) {
                this.widgetObj.destroy.bind(this.widgetObj)();
            }
            else {
                this.destroy();
            }
            this.panelContainerWidget.remove();
        };
        return FullPanel;
    }(BaseWidget));
    return FullPanel;
});
